package cn.caishen.report.controller;

import cn.caishen.report.common.exceptions.BusinessException;
import cn.caishen.report.common.exceptions.ResponseResult;
import cn.caishen.report.common.utils.LbMap;
import cn.caishen.report.domain.system.Module;
import cn.caishen.report.service.system.ModuleService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(value = "/module")
@Slf4j
@Api(tags = "模块")
public class ModuleController {

    private final ModuleService moduleService;

    public ModuleController(ModuleService moduleService) {
        this.moduleService = moduleService;
    }

    /**
     * 菜单分页查询列表
     *
     * @param page 页码
     * @param limit 单页条数
     * @param jsonStr 查询条件
     * @return LbMap
     */
    @GetMapping(value = "/list", name = "菜单分页查询列表")
    @ApiOperation("菜单分页查询列表")
    public LbMap list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, @RequestParam(required = false) String jsonStr){
        try {
            LbMap param = LbMap.fromObject(jsonStr);
            PageInfo<Module> pages = moduleService.getList(page, limit, param);
            log.info("菜单分页查询列表成功");
            return ResponseResult.success("菜单分页查询列表成功", pages.getList());
        }catch (BusinessException e){
            log.info("菜单分页查询列表失败：" + e.getMessage());
            return ResponseResult.fail("菜单分页查询列表失败：" + e.getMessage());
        }catch (Exception e){
            log.info("菜单分页查询列表失败" + e.getMessage());
            return ResponseResult.fail("菜单分页查询列表失败");
        }
    }

    /**
     * 菜单查询列表
     *
     * @param jsonStr 查询条件
     * @return LbMap
     */
    @GetMapping(value = "/allList", name = "菜单查询列表")
    @ApiOperation("菜单查询列表")
    public LbMap allList(@RequestParam(required = false) String jsonStr){
        try {
            LbMap param = LbMap.fromObject(jsonStr);
            List<Module> list = moduleService.getAllList(param);
            log.info("菜单查询列表成功");
            return ResponseResult.success("菜单查询列表成功", list);
        }catch (BusinessException e){
            log.info("菜单查询列表失败：" + e.getMessage());
            return ResponseResult.fail("菜单查询列表失败：" + e.getMessage());
        }catch (Exception e){
            log.info("菜单查询列表失败" + e.getMessage());
            return ResponseResult.fail("菜单查询列表失败");
        }
    }

    /**
     * 菜单通过编号查询
     *
     * @param primaryKey
     * @return LbMap
     */
    @GetMapping(value = "/getById", name = "菜单通过编号查询")
    @ApiOperation("菜单通过编号查询")
    public LbMap getById(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到菜单编号");
            }

            Module module = moduleService.getById(primaryKey);
            log.info("菜单通过编号查询成功");
            return ResponseResult.success("菜单通过编号查询成功", module);
        }catch (BusinessException e){
            log.info("菜单通过编号查询失败：" + e.getMessage());
            return ResponseResult.fail("菜单通过编号查询失败：" + e.getMessage());
        }catch (Exception e){
            log.info("菜单通过编号查询失败" + e.getMessage());
            return ResponseResult.fail("菜单通过编号查询失败");
        }
    }

    /**
     * 修改菜单（新增、修改）
     *
     * @param module module
     * @return LbMap
     */
    @PostMapping(value = "/edit", name = "修改菜单")
    @ApiOperation("修改菜单（新增、修改）")
    public LbMap edit(@RequestBody Module module){
        try {
            if (Objects.isNull(module.getModuleId()) || module.getModuleId()<=0){
                //新增
                moduleService.saveOne(module);
                log.info("菜单新增成功");
            }else {
                //编辑
                moduleService.update(module);
                log.info("菜单修改成功");
            }
            return ResponseResult.success("菜单编辑成功");
        }catch (BusinessException e){
            log.info("菜单编辑失败：" + e.getMessage());
            return ResponseResult.fail("菜单编辑失败：" + e.getMessage());
        }catch (Exception e){
            log.info("菜单编辑失败：" + e.getMessage());
            return ResponseResult.fail("菜单编辑失败");
        }
    }

    /**
     * 根据id删除菜单
     *
     * @param primaryKey id
     * @return LbMap
     */
    @PostMapping(value = "/delete", name = "删除菜单")
    @ApiOperation("删除菜单")
    public LbMap delete(String primaryKey){
        try {
            if (Objects.isNull(primaryKey) || primaryKey.length()==0){
                return ResponseResult.fail("没有找到菜单编号");
            }
            moduleService.delete(primaryKey);
            log.info("菜单删除成功");
            return ResponseResult.success("菜单删除成功");
        } catch (BusinessException e){
            log.info("菜单删除失败：" + e.getMessage());
            return ResponseResult.fail("菜单删除失败：" + e.getMessage());
        } catch (Exception e){
            log.info("菜单删除失败：" + e.getMessage());
            return ResponseResult.fail("菜单删除失败");
        }
    }
}
